
#include <BArray>
#include <BReadWrite>
#include <BEntry>
#include <BFolder>
#include <BLabel>
#include <BListItem>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BSplitter>
#include <BMessageBox>

#include "HtmlEditor.h"
#include "MainWindow.h"

MainWindow::MainWindow()
{
	_normalImage = Read<BImage>("data/ui/neon/neon_001.png");
	_changeImage = Read<BImage>("data/ui/neon/neon_004.png");
	_emptyImage = Read<BImage>("data/ui/neon/neon_011.png");

	_fileDialog = new BFileDialog(this);
	_freshDialog = new FreshDialog(this);

	_menu = new BMenu(this);
	_menu->addAction("fresh");
	_menu->addAction("different");

	_freshButton = new BButton("fresh");
	_freshButton->setName("debug");

	_saveButton = new BButton("save");
	_saveButton->setAble(false);
	_saveAllButton = new BButton("save-all");
	_saveAllButton->setAble(false);

	BLabel* includeLabel = new BLabel("include-path");
	_includePathEdit = new BLineEdit();
	_includePathEdit->setWidthStretch(5);
	BLabel* documentLable = new BLabel("document-path");
	_documentPathEdit = new BLineEdit();
	_documentPathEdit->setWidthStretch(5);

	BHBoxLayout* buttonLayout = new BHBoxLayout();
	buttonLayout->addWidget(_freshButton);
	buttonLayout->addWidget(_saveButton);
	buttonLayout->addWidget(_saveAllButton);
	buttonLayout->addSpring(10);
	buttonLayout->addWidget(includeLabel);
	buttonLayout->addWidget(_includePathEdit);
	buttonLayout->addWidget(documentLable);
	buttonLayout->addWidget(_documentPathEdit);
	buttonLayout->addSpring(10);
	buttonLayout->setSizeStretch(BSizeStretch(1.0f, 0.01f));

	_headList = new BListWidget();
	_tabWidget = new BTabWidget();
	_tabWidget->setWidthStretch(4);
	_tabWidget->setTabClosable(true);

	BSplitter* splitter = new BSplitter();
	splitter->addWidget(_headList);
	splitter->addWidget(_tabWidget);

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addLayout(buttonLayout);
	layout->addWidget(splitter);

	connect(_freshButton, Signal_Clicked, &MainWindow::procFreshHeads);
	connect(_saveButton, Signal_Clicked, &MainWindow::procSaveDocument);
	connect(_includePathEdit, Signal_DoubleClicked, &MainWindow::procIncludePathEditDoubleClicked);
	connect(_documentPathEdit, Signal_DoubleClicked, &MainWindow::procDocumentPathEditDoubleClicked);
	connect(_headList, Signal_ItemDoubleClicked, &MainWindow::procHeadFileDoubleClicked);
	connect(_headList, Signal_ItemClicked, &MainWindow::procHeadFileClicked);

	_includePathEdit->setText("../../include");
	_documentPathEdit->setText("../../document/htmls");
	freshHeads();
}
MainWindow::~MainWindow()
{

}

void MainWindow::procFreshHeads()
{
	freshHeads();
}
void MainWindow::procSaveDocument()
{
	FreshDialog* docEdit = dynamic_cast<FreshDialog*>(_tabWidget->current());
	if (docEdit->save())
	{
		_saveButton->setAble(false);
	}
	else
	{

	}
}

void MainWindow::procIncludePathEditDoubleClicked()
{
	if (_fileDialog->execute())
	{
		const BString& fileName = _fileDialog->fileName();
		_includePathEdit->setText(fileName);
		freshHeads();
	}
}
void MainWindow::procDocumentPathEditDoubleClicked()
{
	if (_fileDialog->execute())
	{
		const BString& fileName = _fileDialog->fileName();
		_documentPathEdit->setText(fileName);
		freshHeads();
	}
}
void MainWindow::procHeadFileClicked(BObject* object, const BValue& value)
{
	BListItem* item = value;
	if (item)
		return;
}
void MainWindow::procHeadFileDoubleClicked(BObject* object, const BValue& value)
{
	BListItem* item = value;
	if (!item)
		return;
	const BString& text = item->text();
	if (!_tabWidget->contains(text))
	{
		const BString& htmlName = item->value();
		if (htmlName.empty())
		{
			_freshDialog->open(_includePathEdit->text() + '/' + text);
			_freshDialog->load(_documentPathEdit->text() + '/' + text + ".html");
			if (_freshDialog->execute())
			{
				item->setValue(htmlName);
			}
			else
			{
				return;
			}
		}

		HtmlEditor* editor = new HtmlEditor();
		editor->open(_documentPathEdit->text() + '/' + text + ".html");
		_tabWidget->addTab(editor);
	}
	_tabWidget->setCurrent(text);
}

void MainWindow::freshHeads()
{
	_includeFolder.setPath(_includePathEdit->text());
	if (_includeFolder.open() == false)
		return;

	_documentFolder.setPath(_documentPathEdit->text());
	if (_documentFolder.open() == false)
		return;

	_headList->clearItems();
	for (int i = 0; i < _includeFolder.entryCount(); i++)
	{
		BEntry* entry = _includeFolder.entry(i);
		if (entry->attrib() & Attrib_File)
		{
			BListItem* item = _headList->addItem(entry->name());
			item->setValue(entry);
			if (BEntry* htmlEntry = _documentFolder.entry(entry->name() + ".html"))
			{
				BString htmlFileName = htmlEntry->path() + entry->name() + ".html";
				if (BFile::Exists(htmlFileName))
				{
					item->setImage(_normalImage);
					item->setValue(htmlFileName);
				}
				else
				{
					item->setImage(_changeImage);
					item->setValue(BValue());
				}
			}
			else
			{
				item->setImage(_emptyImage);
			}
		}
	}
}

void MainWindow::updateEvent(const BEvent& event)
{
	if (!_saveButton->able() && _tabWidget->tabCount())
	{
		BWidget* widget = _tabWidget->current();
		if (widget->title().contain('*'))
			_saveButton->setAble(true);
	}
	if (!_saveAllButton->able() && _saveButton->able())
	{
		_saveAllButton->setAble(true);
	}
}
